<template>
	<el-dialog v-model="visible" destroy-on-close :title="dataForm.valueId ? $t('common.editBtn') : $t('common.addBtn')"
		width="600" @close="closeFn">
		<el-form ref="dicDialogFormRef" :model="dataForm" label-width="120px" :rules="dataRules" v-loading="loading">
			<!--<el-form-item :label="$t('prodPropValue.propId')" prop="propId">-->
			<!--	<el-input v-model="dataForm.propId" clearable disabled></el-input>-->
			<!--</el-form-item>-->
			<el-form-item :label="$t('ProdProp.propName')" prop="propName">
				<el-input v-model="dataForm.propName" clearable disabled></el-input>
			</el-form-item>
			<el-form-item :label="$t('ProdPropValue.valueName')" prop="valueName">
				<el-input v-model="dataForm.valueName" :placeholder="$t('ProdPropValue.inputPropValueTip')"
					clearable></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<span class=" dialog-footer">
				<el-button @click="visible = false">{{ $t('common.cancelButtonText') }}</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">{{ $t('common.confirmButtonText')
				}}</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="dict-item-form">
import { useI18n } from 'vue-i18n';
import { getItemObj, addItemObj, putItemObj, validateDictItemLabel } from '/@/api/bizs/ProdProp';
import { useMessage } from '/@/hooks/message';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
const { t } = useI18n();

// 定义变量内容
const dicDialogFormRef = ref();

const visible = ref(false);
const loading = ref(false);

const dataForm = reactive({
	valueId: '',
	valueName: '',
	propId: '',
	propName: '',
});
const closeFn = () => {
	dicDialogFormRef.value?.resetFields();
	
}


const dataRules = reactive({
	valueId: [{ required: true, message: '属性值id不能为空', trigger: 'blur' }],

	propId: [{ required: true, message: '属性id不能为空', trigger: 'blur' }],
	propName: [{ required: true, message: '属性名称不能为空', trigger: 'blur' }],

	valueName: [
		{ required: true, message: '标签不能为空', trigger: 'blur' },
		{
			validator: (rule: any, value: any, callback: any) => {
				validateDictItemLabel(rule, value, callback, dataForm.valueName, dataForm.propId !== '');
			},
			trigger: 'blur',
		},
	],
});

// 打开弹窗
const openDialog = (row: any, form: any) => {
	visible.value = true;
	dataForm.valueId = '';

	dicDialogFormRef.value?.resetFields();

	if (row?.valueId) {
		getItemObj(row.valueId).then((res) => {
			Object.assign(dataForm, res.data);
		});
	} else if (form) {
		console.log('form: ', form);
		dataForm.propId = form.propId;
		dataForm.propName = form.propName;

	}
};

// 提交
const onSubmit = async () => {
	const valid = await dicDialogFormRef.value.validate().catch(() => { });
	if (!valid) return false;

	try {
		loading.value = true;
		dataForm.valueId ? await putItemObj(dataForm) : await addItemObj(dataForm);
		useMessage().success(t(dataForm.valueId ? 'common.editSuccessText' : 'common.addSuccessText'));
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
